<?php
/**
 * 汽车之家车型大全类库
 * @Copyright (C) 2019 比物福API All rights reserved.
 * @License http://tool.bitefu.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
class Autohomeapi{
    public function __construct(){
        $username = 'root';
        $password = 'root';//密码
        $database = 'car';
        
        $sql = new txtSQL('./data') or @ob_get_clean().die($sql->get_last_error());
        $sql->connect($username, $password) or @ob_get_clean().die($sql->get_last_error());
        $sql->selectdb($database);
        $this->db = $sql;
    }
    //获取品牌信息
    public function brand($postdata){
        $return=array('status'=>0,'info'=>'');
        $sql="SELECT * FROM car_brand";
        $sql_total="select id from car_brand";
        $sql_ext='';
        $map=array();
        if(!empty($postdata['id'])){
            $sql_ext[]="id = ".$postdata['id'];
        }
        if(!empty($postdata['keyword'])){
            $sql_ext[]="name =~ '%".$postdata['keyword']."%'";
        }
        $start=($postdata['page']-1)*$postdata['pagesize'];
        $limit = $start.','.$postdata['pagesize'];
        if(!empty($postdata['order']) && in_array($postdata['order'],array('id','name','firstletter'))){
            $order=$postdata['order'].' desc';
        }else{
            $order='firstletter,id desc';
        }
        if(!empty($sql_ext))$sql_ext=' WHERE '.implode(' and ',$sql_ext);
        $count=$this->db->query($sql_total.$sql_ext);$total['total']=count($count);
        if(empty($total['total'])){
            $result['info']='没有查询到数据';
            return $result;
        }
        $list=$this->db->query($sql.$sql_ext.' order by '.$order.' limit '.$limit);
        if(empty($list)){
            $result['info']='没有查询到数据';
            return $result;
        }
        $result['status']=1;
        $result['info']=$list;
        $result['total']=$total['total'];
        return $result;
    }
    /**
     * 获取车系信息
     * @param $brandid 品牌ID
     */
    public function series($postdata){
        $return=array('status'=>0,'info'=>'');
        $sql="select * from car_series";
        $sql_total="select id from car_series";
        $sql_ext='';
        if(!empty($postdata['id'])){
            $sql_ext[]='id = '.$postdata['id'];
        }
        if(!empty($postdata['keyword'])){
            $sql_ext[]="full_name =~ '%".$postdata['keyword']."%'";
        }
        if(!empty($postdata['brand_id'])){
            $sql_ext[]='brand_id = '.$postdata['brand_id'];
        }
        if(!empty($postdata['group_id'])){
            $sql_ext[]='group_id = '.$postdata['group_id'];
        }
        $start=($postdata['page']-1)*$postdata['pagesize'];
        $limit = $start.','.$postdata['pagesize'];
        if(!empty($postdata['order']) && in_array($postdata['order'],array('id','name','firstletter'))){
            $order=$postdata['order'].' desc';
        }else{
            $order='id desc';
        }
        if(!empty($sql_ext))$sql_ext=' WHERE '.implode(' and ',$sql_ext);
        $count=$this->db->query($sql_total.$sql_ext);$total['total']=count($count);
        if(empty($total['total'])){
            $result['info']='没有查询到数据';
            return $result;
        }
        $list=$this->db->query($sql.$sql_ext.' order by '.$order.' limit '.$limit);
        if(empty($list)){
            $result['info']='没有查询到数据';
            return $result;
        }
        $result['status']=1;
        $result['info']=$list;
        $result['total']=$total['total'];
        return $result;
    }
    
    /**
     * 获取厂商信息
     * @param $brandid 品牌ID
     */
    public function series_group($postdata){
        $return=array('status'=>0,'info'=>'');
        $sql="select * from car_series_group";
        $sql_total="select id from car_series_group";
        $sql_ext='';
        if(!empty($postdata['id'])){
            $sql_ext[]='id = '.$postdata['id'];
        }
        if(!empty($postdata['keyword'])){
            $sql_ext[]="name =~ '%".$postdata['keyword']."%'";
        }
        if(!empty($postdata['brand_id'])){
            $sql_ext[]='brand_id = '.$postdata['brand_id'];
        }
        $start=($postdata['page']-1)*$postdata['pagesize'];
        $limit = $start.','.$postdata['pagesize'];
        if(!empty($postdata['order']) && in_array($postdata['order'],array('id','name','firstletter'))){
            $order=$postdata['order'].' desc';
        }else{
            $order='id desc';
        }
        if(!empty($sql_ext))$sql_ext=' WHERE '.implode(' and ',$sql_ext);
        $count=$this->db->query($sql_total.$sql_ext);$total['total']=count($count);
        if(empty($total['total'])){
            $result['info']='没有查询到数据';
            return $result;
        }
        $list=$this->db->query($sql.$sql_ext.' order by '.$order.' limit '.$limit);
        if(empty($list)){
            $result['info']='没有查询到数据';
            return $result;
        }
        $result['status']=1;
        $result['info']=$list;
        $result['total']=$total['total'];
        return $result;
    }
    /**
     * 获取车型信息
     * @param $postdata 参数
     */
    public function info($postdata){
        $return=array('status'=>0,'info'=>'');
        $sql="select * from car_info";
        $sql_total="select id from car_info";
        $sql_ext='';
        if(!empty($postdata['id'])){            
            $sql_ext[]='id = '.$postdata['id'];
        }
        if(!empty($postdata['keyword'])){
            $sql_ext[]="full_name =~ '%".$postdata['keyword']."%'";
        }
        if(!empty($postdata['brand_id'])){
            $sql_ext[]='brand_id = '.$postdata['brand_id'];
        }
        if(!empty($postdata['group_id'])){
            $sql_ext[]='group_id = '.$postdata['group_id'];
        }
        if(!empty($postdata['series_id'])){
            $sql_ext[]='series_id = '.$postdata['series_id'];
        }
        if(!empty($postdata['year'])){
            $sql_ext[]='year = '.$postdata['year'];
        }
        $start=($postdata['page']-1)*$postdata['pagesize'];
        $limit = $start.','.$postdata['pagesize'];
        if(!empty($postdata['order']) && in_array($postdata['order'],array('id','name'))){
            $order=$postdata['order'].' desc';
        }else{
            $order='id desc';
        }
        if(!empty($sql_ext))$sql_ext=' WHERE '.implode(' and ',$sql_ext);
        $count=$this->db->query($sql_total.$sql_ext);$total['total']=count($count);
        if(empty($total['total'])){
            $result['info']='没有查询到数据';
            return $result;
        }
        $list=$this->db->query($sql.$sql_ext.' order by '.$order.' limit '.$limit);
        if(empty($list)){
            $result['info']='没有查询到数据';
            return $result;
        }
        $result['status']=1;
        $result['info']=$list;
        $result['total']=$total['total'];
        return $result;
    }
    //获取车型年份信息
    public function infoyear($postdata){
        $return=array('status'=>0,'info'=>'');
        $sql_ext='';
        if(!empty($postdata['id'])){            
            $sql_ext[]='id = '.$postdata['id'];
        }
        if(!empty($postdata['keyword'])){
            $sql_ext[]="full_name =~ '%".$postdata['keyword']."%'";
        }
        if(!empty($postdata['brand_id'])){
            $sql_ext[]='brand_id = ' .$postdata['brand_id'];
        }
        if(!empty($postdata['group_id'])){
            $sql_ext[]='group_id = '.$postdata['group_id'];
        }
        if(!empty($postdata['series_id'])){
            $sql_ext[]='series_id = '.$postdata['series_id'];
        }
        if(!empty($sql_ext))$sql_ext=' WHERE '.implode(' and ',$sql_ext);
        $sql="select year from car_info";        
        $res=$this->db->query($sql.$sql_ext.' order by `year` desc');
        if(empty($res)){
            $result['info']='没有查询到数据';
            return $result;
        }$list=array();
        foreach($res as $v){$list[]=$v['year'];}
        $list=array_filter(array_unique($list));arsort($list);
        $result['status']=1;
        $result['info']=array_values($list);
        $result['total']=count($list);
        return $result;
    }
}
?>